var config = require('../common/config.js');

function formatTime(date) {
    var year = date.getFullYear()
    var month = date.getMonth() + 1
    var day = date.getDate()

    var hour = date.getHours()
    var minute = date.getMinutes()
    var second = date.getSeconds()


    return [year, month, day].map(formatNumber).join('/') +
        ' ' + [hour, minute, second].map(formatNumber).join(':')
}

function formatNumber(n) {
    n = n.toString()
    return n[1] ? n : '0' + n
}

/**
 * 获取系统信息,获取成功后:
 * 1.存入本地存储;
 * 2.存入App.globalData内存
 * @param successCb Function
 */
function getSystemInfo(successCb) {
    var app = getApp();
    var info = app.globalData.systemInfo;
    if (info) {
        typeof successCb == 'function' && successCb(info);
        return info;
    }
    wx.getSystemInfo({
        success: function (res) {
            wx.setStorage({
                key: config.storage.keySystemInfo,
                data: res
            });
            app.globalData.systemInfo = res;
            typeof successCb == 'function' && successCb(res);
        },
        fail: function (res) {
        },
        complete: function (res) {
        },
    });
}

/**
 * 获取本地存储的SessionId
 */
function getSessionId() {
    var sessionId;
    try {
        sessionId = wx.getStorageSync(config.storage.keySessionId);
    } catch (e) {
    }
    return sessionId;
}


module.exports = {
    formatTime: formatTime,
    getSystemInfo: getSystemInfo,
    getSessionId: getSessionId
};
